package cn.raven.controller;

import cn.raven.entity.User;
import cn.raven.handler.UserBlock;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope  //刷新配置
public class UserController {

    @Value("${test}")
    private Long test;

    /*
        1.当触发了配置的规则 那么sentinel会抛出blockException
        2.当配置了blockHandler的时候 那么就会捕获blockException 并进入指定的方法
        3.当配置了fallback,那么程序出现异常 就会进入指定的方法
        4.如果fallback和blockHandler同时触发 那么会进到blockHandler指定的方法
      */
    @GetMapping("/getUser/{id}")
    @SentinelResource(value = "user",
            blockHandler = "getUserBlock",
            blockHandlerClass = UserBlock.class,
    fallback = "userFallBack")
    public User getUser(@PathVariable Long id){
        return User.builder().id(test).name("天霸"+id).intor("是个大傻子").build();
    }

    public User userFallBack(@PathVariable Long id){
        return User.builder().id(id).name("fallBack").intor("fallBack降级").build();
    }

}
